package test;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Base64;

/**
 * Created by Fenghua Qiu on 1/17/2017.
 */
public class AESDecode {


	public static void decode(String array,String secretKey) throws Exception{
		KeyGenerator key =KeyGenerator.getInstance("AES");
		key.init(128,new SecureRandom(secretKey.getBytes()));
		SecretKey secretKey1 = key.generateKey();
		byte[] encodeformat = secretKey1.getEncoded();
		SecretKeySpec se = new SecretKeySpec(encodeformat,"AES");
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.DECRYPT_MODE, se);
		//String s = new String(cipher.doFinal(array),"UTF-8");
		//System.out.println(s);


		Base64.Decoder decoder = Base64.getDecoder();
		byte[] data =decoder.decode(array);

	}
//
//	private static void decode2(){
//		try {
//			byte[] raw = sKey.getBytes("ASCII");
//			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
//			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//			IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
//			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
//			byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
//			byte[] original = cipher.doFinal(encrypted1);
//			String originalString = new String(original,encodingFormat);
//			return originalString;
//		} catch (Exception ex) {
//			return null;
//		}
//	}
	public static void main(String[] args) throws Exception{
		String session = "s8I1Y0Q9yPS5jsGJaZ2sBg==";
		String data ="CqeH/aroUdIGqKzhLTbgOzzz5BwpQ3+hFZRa+GH1qWvgMAsMhjU0tUDMmYQ7CFEbzf8mQ9CeADP3RW1imfoEJJ+xTJWciIPYJ1FiNZsVVTNlGrVC5YuLKy+QOESnPv7JIUKuM7KrWuVXWER8G4GTCKs2OGcP/RtrDUK4G1zGeKEO/UbuiwIlCHYCfaIHppKF7TjxUL36RyW29xFHuWyqC/z1Q4yX5oGAIUbG1UXWIJXy/WHceN19imBKKpBhDbqhpbhBmgRrfhaClOJCXocRGj1LqRiLhL29Xo9zw3hhWeEQ806owM6nJC99WSxW7y8Za8OSAcC+bvvtzxuBAntrh4uml/85DX12mquYb0XTpcn5GECGAYGojgddzxgey4Bx6Dh+QK68fVLy/1b0OtqF7tIWuFe9SqYYMGwmkTe6gbr8J9X6JI+1epG/cfItONQSYm7g0g+ahMiLZ8jxKIBqjA==";
		String iv ="wrbVnZIxT7HgfQHYzAh89g";
		Base64.Decoder base64 = Base64.getDecoder();
		byte[]  b=     decrypt(base64.decode(data),base64.decode(session),base64.decode(iv));
		String  s = new String(b);
		System.out.println("infomationis"+s);



	}

	public static  byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {
		//initialize();
		try {
			Security.addProvider(new BouncyCastleProvider());
			Key sKeySpec = new SecretKeySpec(keyByte, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
			cipher.init(Cipher.DECRYPT_MODE, sKeySpec,new IvParameterSpec(ivByte));// 初始化
			byte[] result = cipher.doFinal(content);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


}
